/*
 * @lc app=leetcode.cn id=2287 lang=typescript
 *
 * [2287] 重排字符形成目标字符串
 */

// @lc code=start
function rearrangeCharacters(s: string, target: string): number {
    const map = new Map()
    for (let i = 0; i < s.length; i++) {
        const num = map.get(s[i])
        if (num) {
            map.set(s[i], num + 1)
        } else {
            map.set(s[i], 1)
        }
    }

    const tMap = new Map();
    for (let i = 0; i < target.length; i++) {
        const num = tMap.get(target[i])
        if (num) {
            tMap.set(target[i], num + 1)
        } else {
            tMap.set(target[i], 1)
        }
    }
    
    let minNum = Number.MAX_SAFE_INTEGER;
    for (const key of tMap.keys()) {
        const n1 = tMap.get(key);
        const n2 = map.get(key);
        if (n1 && n2) {
            minNum = Math.min(minNum, Math.floor(n2 / n1))
        } else {
            return 0
        }
    }
    return minNum;
};

// @lc code=end

